﻿layui.config({
    base: './js/' //你的扩展模块所在目录
}).use(['jquery','carousel','flow','layer','ajaxRequest','flow'], function () {
    var $ = layui.jquery;
    var flow = layui.flow;
    var layer = layui.layer;
    var ajaxRequest = layui.ajaxRequest;
    var flow = layui.flow;

    var width = width || window.innerWeight || document.documentElement.clientWidth || document.body.clientWidth;
    width = width>1200 ? 1170 : (width > 992 ? 962 : width);
    var carousel = layui.carousel;
    //建造实例
    carousel.render({
      elem: '#carousel'
      ,width: width+'px' //设置容器宽度
      ,height:'360px'
      ,indicator: 'inside'
      ,arrow: 'always' //始终显示箭头
      ,anim: 'default' //切换动画方式
      
    });

    // 喇叭广播点击事件
    $(".home-tips-container span").click(function(){
        layer.msg($(this).text(), {
            time: 5000, //20s后自动关闭
            btn: ['明白了']
        });
    });

    // 最近留言点击事件
    $("body").off('mouseover','.recent-list .hotusers-list-item').on("mouseover", ".recent-list .hotusers-list-item", function () {
        var name = $(this).children(".remark-user-avator").attr("data-name");
        var str = "【"+name+"】的评论";
        layer.tips(str, this, {
            tips: [2,'#666666']
        });
    });
    /*$(".recent-list .hotusers-list-item").mouseover(function () {
        var name = $(this).children(".remark-user-avator").attr("data-name");
        var str = "【"+name+"】的评论";
        layer.tips(str, this, {
            tips: [2,'#666666']
        });
    });*/

    // QQ等图标悬浮事件
    $("#QQjl").mouseover(function(){
        layer.tips('QQ交流', this,{
            tips: 1
        });
    });
    $("#gwxx").mouseover(function(){
        layer.tips('给我写信', this,{
            tips: 1
        });
    });
    $("#xlwb").mouseover(function(){
        layer.tips('Git', this,{
            tips: 1
        });
    });
    $("#htgl").mouseover(function(){
        layer.tips('后台管理', this,{
            tips: 1
        });
    });
    
    $(function () {

        $(".fa-home").parent().parent().addClass("layui-this");
        //播放公告
        playAnnouncement(5000);
        // 加载文章列表
        initArticle();
        // 加载作者信息
        getAuthorInfo();
        // 加载友情链接
        getFriendLinkList();
        // 加载排行榜数据
        initRanking();
        // 加载站长推荐数据
        initRecommended();
        // 加载热评用户
        getBuzzUser();
        // 加载最近留言
        getRecentlyComment();
    });

    // 播放公告
    function playAnnouncement(interval) {
        var index = 0;
        var $announcement = $('.home-tips-container>span');
        //自动轮换
        setInterval(function () {
            index++;    //下标更新
            if (index >= $announcement.length) {
                index = 0;
            }
            $announcement.eq(index).stop(true, true).fadeIn().siblings('span').fadeOut();  //下标对应的图片显示，同辈元素隐藏
        }, interval);
    }

    // 加载文章
    function initArticle() {
        flow.load({
            elem: '#parentArticleList' //指定列表容器
            // 放开下面两个注解为手动加载
            // ,isAuto: false
            // ,isLazyimg: true
            ,done: function(page, next){ //到达临界点（默认滚动触发），触发下一页
                var lis = [];
                //以jQuery的Ajax请求为例，请求下一页数据（注意：page是从2开始返回）
                $.ajax({
                    url:baseUrl + "/blog/article/getIndexArticleList",
                    data:{'page':page,'limit':indexLimit},
                    type:"GET",
                    async:true,
                    success:function (res) {
                        var total = res.data.total;
                        var list = res.data.rows;

                        $.each(list,function(index,element){

                            // 置顶和原创显示
                            var zhiding = '';
                            var yuancheng = '';
                            if (element.top == 1) {
                                zhiding = '<span class="article_is_top">置顶</span>&nbsp;'
                            }
                            if (element.original == 1){
                                yuancheng = '<span class="article_is_yc">原创</span>&nbsp;'
                            }

                            // 内容截取
                            var content = deleteHtmlTags(element.content);
                            var len  = content.length > 85 ? 86 : content.length;
                            content = content.substr(0,len);

                            lis.push('<div class="article shadow animated zoomIn">\n' +
                                '\t\t\t\t\t\t\t\t<div class="article-left ">\n' +
                                '\t\t\t\t\t\t\t\t\t<img src="'+element.articleCover+'" alt="'+element.title+'">\n' +
                                '\t\t\t\t\t\t\t\t</div>\n' +
                                '\t\t\t\t\t\t\t\t<div class="article-right">\n' +
                                '\t\t\t\t\t\t\t\t\t<div class="article-title">\n' + zhiding + yuancheng +
                                '\t\t\t\t\t\t\t\t\t\t<a href="'+articleUrl + element.id+'.html?watchNumber='+element.watchNumber+'">'+element.title+'</a>\n' +
                                '\t\t\t\t\t\t\t\t\t</div>\n' +
                                '\t\t\t\t\t\t\t\t\t<div class="article-abstract">\n' +
                                ''+content+'</div>\n' +
                                '\t\t\t\t\t\t\t\t</div>\n' +
                                '\t\t\t\t\t\t\t\t<div class="clear"></div>\n' +
                                '\t\t\t\t\t\t\t\t<div class="article-footer">\n' +
                                '\t\t\t\t\t\t\t\t\t<span><i class="fa fa-clock-o"></i>&nbsp;&nbsp;'+element.createdTime+'</span>\n' +
                                '\t\t\t\t\t\t\t\t\t<span class="article-author"><i class="fa fa-user"></i>&nbsp;&nbsp;\' '+element.author+'</span>\n' +
                                '\t\t\t\t\t\t\t\t\t<span><i class="fa fa-tag"></i>&nbsp;&nbsp;<a href="javascript:void(0);"> '+element.categoryName+'</a></span>\n' +
                                '\t\t\t\t\t\t\t\t\t<span class="article-viewinfo"><i class="fa fa-eye"></i>&nbsp;'+element.watchNumber+'</span>\n' +
                                '\t\t\t\t\t\t\t\t\t<span class="article-viewinfo"><i class="fa fa-commenting"></i>&nbsp;'+element.commentNumber+'</span>\n' +
                                '\t\t\t\t\t\t\t\t</div>\n' +
                                '\t\t\t\t\t\t\t</div>');
                        });

                        next(lis.join(''), page < Math.ceil(total/indexLimit));
                    },
                    error:function (res) {

                    }
                });

            }
        });

    }

    // 加载排行榜数据
    function initRanking() {
        var res = ajaxRequest.sendGet("/blog/article/getRankingArticleList",{'limit':rankingAndRecom},false,false);
        var html = "";
        if (res) {
            $.each(res.data,function(index,element){

                var classa = "";
                if (index == 0){
                    classa = "layui-bg-red";
                } else if (index == 1) {
                    classa = "layui-bg-orange";
                }else if (index == 2) {
                    classa = "layui-bg-green";
                }

                html = html +
                    "<li>\n" +
                    "<span>\n" +
                    "<i class=\"layui-badge-rim "+classa+"\">"+Number(index+1)+"</i>\n" +
                    "</span> &nbsp;&nbsp;\n" +
                    "<a href=\""+articleUrl + element.id + ".html?watchNumber="+element.watchNumber+" \">"+element.title+"</a>\n" +
                    "</li>";

            });
            $("#ranking").html(html);
        }
    }

    // 加载站长推荐数据
    function initRecommended() {
        var res = ajaxRequest.sendGet("/blog/article/getRecommendedArticleList",{'limit':rankingAndRecom},false,false);
        var html = "";
        if (res) {
            $.each(res.data,function(index,element){
                html = html +"<li>\n" +
                    "<span class=\"article_is_yc\">原创</span> &nbsp;&nbsp;\n" +
                    "<a href=\""+articleUrl + element.id + ".html?watchNumber="+element.watchNumber+" \">"+element.title+"</a>\n" +
                    "</li>";
            });
            $("#recommended").html(html);
        }
    }

    // 加载热评用户
    function getBuzzUser(){
        var res = ajaxRequest.sendGet("/blog/article/getBuzzUser",{},false,false);
        var html = "";
        if (res) {
            $.each(res.data,function(index,element){
                html = html +"<li class=\"hotusers-list-item\">\n" +
                    "\t\t\t\t\t\t\t\t\t<div class=\"hotusers-top hotusers-num\">"+Number(index+1)+"</div>\n" +
                    "\t\t\t\t\t\t\t\t\t<div class=\"hotusers-avator\">\n" +
                    "\t\t\t\t\t\t\t\t\t\t<img src=\""+element.headPortrait+"\" width=\"45\" height=\"45\">\n" +
                    "\t\t\t\t\t\t\t\t\t</div>\n" +
                    "\t\t\t\t\t\t\t\t\t<div>\n" +
                    "\t\t\t\t\t\t\t\t\t\t<div class=\"hotusers-nick\">"+element.nickname+"</div>\n" +
                    "\t\t\t\t\t\t\t\t\t\t<span class=\"hotusers-level\" title=\"等级1\">\n" +
                    "                                            <i style=\"\">活跃</i></span>\n" +
                    "\t\t\t\t\t\t\t\t\t\t<span class=\"hotusers-totalcmt\">本站评论数："+element.commentNum+"</span>\n" +
                    "\t\t\t\t\t\t\t\t\t</div>\n" +
                    "\t\t\t\t\t\t\t\t\t<span class=\"hotusers-icons\"></span>\n" +
                    "\t\t\t\t\t\t\t\t</li>";
            });
            $(".hotusers-list").html(html);
        }
    }

    // 加载最近留言
    function getRecentlyComment(){
        var res = ajaxRequest.sendGet("/blog/article/getRecentlyComment",{},false,false);
        var html = "";
        if (res) {
            $.each(res.data,function(index,element){
                html = html +"<li class=\"hotusers-list-item\">\n" +
                    "\t\t\t\t\t\t\t\t\t<div data-name=\""+element.nickname+"\" class=\"remark-user-avator\">\n" +
                    "\t\t\t\t\t\t\t\t\t\t<img src=\""+element.headPortrait+"\" width=\"45\" height=\"45\">\n" +
                    "\t\t\t\t\t\t\t\t\t</div>\n" +
                    "\t\t\t\t\t\t\t\t\t<div class=\"remark-content\">"+element.content+"</div>\n" +
                    "\t\t\t\t\t\t\t\t\t<span class=\"hotusers-icons\"></span>\n" +
                    "\t\t\t\t\t\t\t\t</li>";
            });
            $(".recent-list").html(html);
        }
    }

    // 加载作者信息
    function getAuthorInfo(){
        var result = ajaxRequest.sendGet("/admin/cache/getAuthorInfo",{},false,true);
        if (result) {
            $("#authorInfo").html(
                "<div class=\"blogerinfo-figure\">\n" +
                "                                    <img src=\""+result.data.headPortrait+"\" alt=\""+blogName+"\">\n" +
                "                                </div>\n" +
                "                                <p class=\"blogerinfo-nickname\">"+result.data.nickname+"</p>\n" +
                "                                <p class=\"blogerinfo-introduce\">"+result.data.motto+"up up~</p>\n" +
                "                                <p class=\"blogerinfo-location\"><i class=\"fa fa-location-arrow\"></i>&nbsp;<span>"+result.data.latelyLoginAddress+"</span></p>"
            );
        }
    }

    // 加载友情链接
    function getFriendLinkList(){
        var result = ajaxRequest.sendGet("/admin/cache/getFriendLinkList",{},false,true);
        if (result) {
            var html = "";
            $.each(result.data,function(index,element){
                html = html +"<li>\n" +
                    "\t\t\t\t\t\t\t\t\t<a target=\"_blank\" href=\""+element.linkUrl+"\" title=\""+element.linkName+"\">"+element.linkName+"</a>\n" +
                    "\t\t\t\t\t\t\t\t</li>"
            })
            $("#blogroll").html(html);
        }
    }
});

// 点击文章信息中的分类时的点击时间
function classifyList(id){
    // console.log(id);
    layer.msg('功能要自己写');
}

// 将字符串中的html标签进行替换
function deleteHtmlTags(htmlStr){
    //定义script的正则表达式，去除js可以防止注入
    /*var scriptRegex = /<script[^>]*?>[\\s\\S]*?<\\/script>/gm;
    //定义style的正则表达式，去除style样式，防止css代码过多时只截取到css样式代码
    var styleRegex = /<style[^>]*?>[\\s\\S]*?<\\/style>/gm;*/
    //定义HTML标签的正则表达式，去除标签，只提取文字内容
    // 排除所有html标签
    // var htmlRegex = /<[^>]+>/gm;
    var regx = /<[^>]*>|<\/[^>]*>/gm;
    // 排除除了img、p、span标签以外的所有标签
    // var htmlRegex = "(?!<(img|p|span).*?>)<.*?>";
    // var htmlRegex = "(?!<(img).*?>)<.*?>";
    //定义空格,回车,换行符,制表符
    var spaceRegex = /\\s*|\t|\r|\n/gm;
    // 过滤script标签
    // htmlStr = htmlStr.replace(scriptRegex, "");
    // 过滤style标签
    // htmlStr = htmlStr.replace(styleRegex, "");
    // 过滤html标签
    // htmlStr = htmlStr.replace(htmlRegex, "");
    htmlStr = htmlStr.replace(regx, "");
    // 过滤空格等
    htmlStr = htmlStr.replace(spaceRegex, "");
    // 过滤&nbsp;
    htmlStr = htmlStr.replace("&nbsp;", "");
    // 过滤&nbsp
    htmlStr = htmlStr.replace("&nbsp", "");
    // 返回文本字符串
    htmlStr = htmlStr.trim();
    //去除空格" "
    htmlStr = htmlStr.replaceAll(" ", "");

    return htmlStr;
}



